-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
37 lines (31 loc) · 1.09 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.Scanner;
public class PairsWithSum {
public static void findPairs(int[] arr, int target) {
boolean found = false;
System.out.println("Pairs with sum " + target + ":");
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] + arr[j] == target) {
System.out.println("(" + arr[i] + ", " + arr[j] + ")");
found = true;
}
}
}
if (!found) {
System.out.println("No pairs found.");
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int n = scanner.nextInt();
int[] arr = new int[n];
System.out.print("Enter " + n + " elements of the array: ");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
System.out.print("Enter the target sum: ");
int target = scanner.nextInt();
findPairs(arr, target);
}
}